import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'

import Layout from "@/views/layout/Layout";

//获取原型对象上的push函数 解决重复点击路由报错（不设也没影响）
const originalPush = Router.prototype.push
//修改原型对象中的push方法
Router.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

Vue.use(Router)

export const constantRouterMap = [
    {
        path: '/',
        component: Layout,
        redirect: '/home',
        name: 'Layout',
        children: [
            {
                path: '/home',
                name: 'Home',
                meta: {title: '首页'},
                isNavigationBar: true,
                component: () => import('@/views/home/Home')
            },
            {
                path: '/archive',
                name: 'Archive',
                meta: {title: '归档'},
                isNavigationBar: true,
                component: () => import('@/views/archive/Archive')
            },
            {
                path: '/aboutMe',
                name: 'AboutMe',
                meta: {title: '关于我'},
                isNavigationBar: true,
                component: () => import('@/views/aboutMe/AboutMe')
            },
            {
                path: '/blogDetail/:id',
                name: 'BlogDetail',
                meta: {title: '文章详情'},
                component: () => import('@/views/blogDetail/BlogDetail')
            }
        ]
    },
    {
        path: '/500', 
        component: () => import('@/views/error/500') 
    }
]

const router = new Router({
    mode: 'history',
    base: process.env.BASE_URL,
    routes: constantRouterMap
})
router.beforeEach((to, from, next) => {
    let title = '阿欣博客'
    if (to.meta.params){
        title = `${to.meta.title}:${to.params[to.meta.params] || ''} - ${title}`
    }else {
        title = `${to.meta.title} - ${title}`
    }
    document.title = title

    next();
})

export default router
